<?

define('MAXNUM', 500);

?>
<?
  require_once('../db.php');
  require_once('../util/geometry_to_array.php');
  require_once('../util/misc.php');
  db_connect();

  $comid = intval($_GET["c"]);

  $query = 'SELECT name_com AS name, codecnt_com AS code, AsText(pointcenter_com) AS center, AsText(pointtl_com) AS tl, AsText(pointbr_com) AS br FROM ldi_comune_com';
  $query .= " WHERE id_com='".$comid."'";
  $result = mysql_query($query) or die("ERROR PERFORMING QUERY '".$query."': ".mysql_error());

  if (!($row = mysql_fetch_assoc($result)))
    die("Invalid comune specified");

  // parse point
  $p = geometry_to_array($row["center"]);
  $cx = $p[0];
  $cy = $p[1];
  $ptl = geometry_to_array($row["tl"]);
  $pbr = geometry_to_array($row["br"]);
  $b_s = $pbr[1];
  $b_w = $ptl[0];
  $b_e = $pbr[0];
  $b_n = $ptl[1];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=false;" />
<title>LDIR alege morman - <?=capitalize($row["name"])?></title>
<link href="style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="../const.js"></script>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="../array.js"></script>
<script type="text/javascript" src="../marker.label.js"></script>
<script type="text/javascript" src="../marker.ldir.js"></script>
<script type="text/javascript" src="../marker.gps.js"></script>
<script type="text/javascript" src="../maptypes.js"></script>
<script type="text/javascript" src="../misc.drag.js"></script>
<script type="text/javascript" src="../misc.util.js"></script>

<script type="text/javascript">

var map;
var markers = [];
var infoWindow;
var centerMarker;
var selectedMarker;
var mapBounds;

var options = {
  version: 1,
  map: {
    gps: false,
    type: 0,
    zoom: 12,
    lat: <?=$cy?>,
    lng: <?=$cx?>
  },
  maxNum: <?=MAXNUM?>
};

var poly_colors = {};
var POLY_COLOR = "#901ad4";

var oldw = 0;
var oldh = 0;

var MAP_TYPE_IDS = [ 'mtMapMaker', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN ];

//############
function resizeHandler() {
  // get height and width
  winH = (navigator.appName.indexOf("Microsoft")!=-1) ? document.body.offsetHeight : window.innerHeight;
  winW = (navigator.appName.indexOf("Microsoft")!=-1) ? document.body.offsetWidth : window.innerWidth;
  var mc = document.getElementById('map_canvas');
  // mc.style.width = (winW - 204) + "px";
  mc.style.width = winW + "px";
  mc.style.height = winH + "px";

  var oldCenter;
  if (oldw != 0 && oldh != 0)
    oldCenter = map.getCenter();

  // map object
  google.maps.event.trigger(map, 'resize');
  // moveHandler();

  oldw = winW;
  oldh = winH;
  if (oldCenter)
    map.setCenter(oldCenter);
}

//############

function addPolyLines() {
  for (var cc in comune_polys) {
    var col = comune_polys[cc].color;
    var pol = comune_polys[cc].poly;
    for (var j=0; j<pol.length; j++) {
      var ll = [];
      for (var k=0; k<pol[j].length; k++)
        ll.push(new google.maps.LatLng(pol[j][k].y, pol[j][k].x));
      var pl = new google.maps.Polygon({map: map, paths: ll, strokeColor: POLY_COLOR, strokeOpacity: 1.0, strokeWeight: 2, fillColor: POLY_COLOR, fillOpacity: 0});
    }
  }
}

//############

function loadJS() {
  var pcscr = document.getElementById("pcscr");
  if (pcscr) document.body.removeChild(pcscr);
  pcscr = document.createElement("SCRIPT");
  if (pcscr) document.body.appendChild(pcscr);
  pcscr.src = 'onecomune.js.php?c=<?=$comid?>';
  pcscr.id = 'pcscr';
}

//############

function loadPlaces() {
  for (var i=0; i<places.length; i++) {
    var p = places[i];
    p.latlng = new google.maps.LatLng(p.lat, p.lng);
  }
  updatePlaces();

  map.fitBounds(mapBounds);
}

//############

function updatePlaces() {
  if (!places)
    return;

  var bounds = map.getBounds();

  // filter
  var newPlaces = [];
  var i;
  for (i=0; i<places.length /* && newPlaces.length <= options.maxNum */; i++) {
    var p = places[i];
    if (pointInPolygon(p.latlng, comune_polys[<?=$comid?>].poly))
      newPlaces.push(p);
  }

  var selectedMarkerFound = !selectedMarker;

  // clear marked
  markers.forEach(function(m) {
    m.marked = false;
  });

  for (var i=0; i<newPlaces.length; i++) {
    var p = newPlaces[i];

    var foundMarker = null;
    try {
      foundMarker = markers.find2(p.id, function(m) { return m.id; });
    }
    catch(err) {
      if (Const.logging) console.log("ERROR -- updatePlaces -- find marker -- " + err);
      foundMarker = null;
    }

    var marker = null;
    if (foundMarker != null) {
      marker = foundMarker;
      marker.marked = true;
      if (selectedMarker) {
        if (marker.id == selectedMarker.id)
          selectedMarkerFound = true;
      }
    }
    else {
      marker = new LDIRMarker(p);
      if (marker.valid) {
        marker.marked = true;
        marker.listener = google.maps.event.addListener(marker, 'click', function(ev) {
          this.markerClicked(ev);
        });
        markers.push(marker);
        marker.setMap(map);
        var label = new Label({ map: map });
        label.marker = marker;
        label.bindTo('position', marker, 'position');
        label.bindTo('text', marker, 'position');
      }
    }
  }

  // sweep
  markers.lastForEach(function(m) {
    try {
      if (!m.marked) {
        markers.remove(m);
        if (m.listener)
          google.maps.event.removeListener(m.listener);
        m.setMap(null);
      }
    }
    catch(err) {
      if (Const.logging) console.log("ERROR -- updatePlaces -- markers.lastForEach -- " + err);
    }
  });

  // gc
  newPlaces.removeAll();

  if (!selectedMarkerFound) {
    infoWindow.close();
    selectedMarker = null;
  }
}

//############

function initialize() {
  DragFix.enableWorkaround();

  // map initialization

  map = new google.maps.Map(
    document.getElementById("map_canvas"),
    {
      zoom: options.map.zoom,
      center: new google.maps.LatLng(options.map.lat, options.map.lng),
      mapTypeId: MAP_TYPE_IDS[options.map.type],
      navigationControl: false,
      mapTypeControl: true,
      mapTypeControlOptions: {
        mapTypeIds: MAP_TYPE_IDS,
        style: google.maps.MapTypeControlStyle.DEFAULT
      },
      scaleControl: true,
      scaleControlOptions: {
        position: google.maps.ControlPosition.BOTTOM_LEFT
      }
    }
  );

  mapBounds = new google.maps.LatLngBounds(new google.maps.LatLng(<?=$b_s?>, <?=$b_w?>), new google.maps.LatLng(<?=$b_n?>, <?=$b_e?>));
  map.fitBounds(mapBounds);

  map.mapTypes.set('mtMapMaker', mtMapMaker);

  // events
  if (window.attachEvent)
    window.attachEvent("onresize", resizeHandler);
  else
    window.addEventListener("resize", resizeHandler, false);

  map.setMapTypeId('mtMapMaker');

  infoWindow = new google.maps.InfoWindow({ content: '' });
  google.maps.event.addListener(infoWindow, "closeclick", function() {
    selectedMarker = null;
    this.close();
  });

  setTimeout('loadJS()', 100);

  resizeHandler();
}

//############

function zoomIn() {
  map.setZoom(map.getZoom()+1);
}

//############

function zoomOut() {
  map.setZoom(map.getZoom()-1);
}

</script>
</head>

<body onload="initialize()">
<div id="wrap_main">
  <div id="map_canvas"></div>
  <div id="msg_div1">
    <p>&nbsp;<?=capitalize($row["name"])?>&nbsp;</p>
  </div>
  <div id="msg_div4">
    <p class="title">Legend&#259; mormane</p>
    <p>&nbsp;<img src="../icons/i1.png" class="small" /> &nbsp;raportate&nbsp;</p>
    <p>&nbsp;<img src="../icons/i2.png" class="small" /> &nbsp;excluse&nbsp;</p>
    <p>&nbsp;<img src="../icons/i3.png" class="small" /> &nbsp;alocate&nbsp;</p>
    <p>&nbsp;<img src="../icons/i7.png" class="small" /> &nbsp;cur&#259;&#355;ate&nbsp;</p>
  </div>
  <div id="zoomin_div">
    <a class="dragfix" href="javascript:zoomIn()"><img src="icons/btn-zoomin.png" alt="Zoom In" /></a>
  </div>
  <div id="zoomout_div">
    <a class="dragfix" href="javascript:zoomOut()"><img src="icons/btn-zoomout.png" alt="Zoom Out" /></a>
  </div>
  <div id="cpr_div">
    <p>Powered by GTS Telecom</p>
  </div>
</div>
</body>
</html>
